﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Techstar.Config;
using System.IO;
using System.Windows.Media.Imaging;
using System.Runtime.Serialization;
using System.ComponentModel;
using Org.Hessian.Utility;

namespace SmartMD.Manager
{
    class SingletonManager
    {
        public const string TemplatePath = "Template";
        public const string TemplateScreenPath = "Template/Screen";
        private static HessianCache _cache, _currentCache;
        /// <summary>
        /// 应用程序所有路径
        /// </summary>
        public static string AppPath
        {
            get
            {
                return Path.GetDirectoryName(Application.ExecutablePath);
            }
        }
        /// <summary>
        /// 当前用户
        /// </summary>
        private static string _currentUser;
        public static string CurrentUser
        {
            get
            {
                return _currentUser;
            }
            set {
                _currentUser = value;
            }
        }

        /// <summary>
        /// 全局配置文件
        /// </summary>
        public static Configuration Config
        {
            get
            {
                return new Configuration(Path.Combine(AppPath, "common.config.xml"));
            }
        }
        public static string Name { get; set; }
        public static HessianCache Cache
        {
            get
            {
                if (_cache != null) return _cache;
                var path = Path.Combine(AppPath, "Cache\\" + Name);
                _cache = new HessianCache(path);
                return _cache;
            }
        }

        public static HessianCache CurrentCache
        {
            get
            {
                if (_currentCache != null) return _currentCache;
                var path = Path.Combine(AppPath, "Cache");
                _currentCache = new HessianCache(path);
                return _currentCache;
            }
        }
        /// <summary>
        /// 是否在线
        /// </summary>
        public static bool Online
        {
            get;
            set;
        }
        public static BitmapImage GetImageResource(Type type, string path)
        {
            var stream = type.Assembly.GetManifestResourceStream(type.Namespace + "." + path);
            if (stream == null) return null;
            var result = new BitmapImage();
            result.BeginInit();
            result.StreamSource = stream;
            result.EndInit();
            return result;
        }

        public static string ContainerPath { get; set; }
    }

    ///////////////////////////////////////////

    [DataContract(Name = "ConnectConfig")]
    public class ConnectConfig : INotifyPropertyChanged
    {
        private string _name;
        private string _hessianUrl;
        private string _pushIP;
        private string _pushPort = "9999";

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        [IgnoreDataMember]
        public string GroupName
        {
            get;
            set;
        }

        [DataMember]
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                this._name = value;
                this.OnPropertyChanged("Name");
            }
        }

        [DataMember]
        public string HessianUrl
        {
            get
            {
                return _hessianUrl;
            }
            set
            {
                this._hessianUrl = value;
                this.OnPropertyChanged("HessianUrl");
            }
        }

        public string PushUrl
        {
            get { return PushIP + ":" + PushPort; }
        }

        [DataMember]
        public string PushIP
        {
            get
            {
                return _pushIP;
            }
            set
            {
                this._pushIP = value;
                this.OnPropertyChanged("PushIP");
                this.OnPropertyChanged("PushUrl");
            }
        }

        [DataMember]
        public string PushPort
        {
            get
            {
                return _pushPort;
            }
            set
            {
                this._pushPort = value;
                this.OnPropertyChanged("PushPort");
                this.OnPropertyChanged("PushUrl");
            }
        }
    }
}
